/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: xclient_impl.h
 *
 * Purpose: client implementation base API define
 *
 * Developer:
 *   wen.gu , 2023-10-25
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __XBUS_COM_INTERNAL_XCLIENT_IMPL_H__
#define __XBUS_COM_INTERNAL_XCLIENT_IMPL_H__

#include <string>
#include <memory>
#include <functional>
#include <map>

#include "xbus/core/xtype.h"
#include "xbus/core/xresult.h"
#include "xbus/core/xqueue.h"
#include "xbus/core/xthread.h"

#include "xbus/com/xtypes.h"
#include "xbus/com/xmessage.h"
#include "xbus/com/xdialer.h"


namespace xbus::com::internal {

class XClientImpl {
public:
    using XResultCode = core::XResultCode;
    using SequenceNumberGenerator = std::function<SequenceNumber()>;
    using OnMessageHandler = std::function<void(XMessageSp)>;
public:
    using MessageMap = std::map<MessageId, XMessageSp>;
    using ResponseMap = std::map<XMessageType, MessageMap>;
    using OnMessageHandlerArray = std::vector<OnMessageHandler>;
    using EventSubscirbeMap = std::map<MessageId, OnMessageHandlerArray>;

    struct EventHandle {
        MessageId event_id;
        OnMessageHandler handle;
    };

    using EventHandleArray = std::vector<EventHandle>;

public:
    virtual ~XClientImpl();
    XClientImpl(XDialerSp dialer);

public:
    virtual XResultCode start(const XClientStartParam& param);
    virtual XResultCode stop();
public:
    virtual core::XResult<XMessageSp> alloc(XMessageType type, MessageId msg_id, uint32_t payload_size, const uint8_t* payload = nullptr);
    virtual XResultCode subscribe(MessageId msg_id, OnMessageHandler event_handler);
    virtual XResultCode subscribe(const EventHandleArray& events);
    virtual XResultCode unsubscribe(MessageId msg_id);
    virtual XResultCode requestAsync(XMessageSp msg_ptr, OnMessageHandler response_handler = nullptr);

protected:
    XDialerSp dialer_;
    EventSubscirbeMap event_handle_;
    ResponseMap response_handle_;
    core::XThread::XThreadPtr event_worker_ = nullptr;

};

} //namespace xbus::com::internal


#endif /** !__XBUS_COM_INTERNAL_XCLIENT_IMPL_H__ */